package com.xzl.org.learning_situation_analysis.controller;

import com.xzl.org.learning_situation_analysis.entity.Analysis.AggregateAnalysis;
import com.xzl.org.learning_situation_analysis.entity.Analysis.ClazzAnalysis;
import com.xzl.org.learning_situation_analysis.entity.GroupManageData;
import com.xzl.org.learning_situation_analysis.entity.Paper;
import com.xzl.org.learning_situation_analysis.entity.QuestionScore;
import com.xzl.org.learning_situation_analysis.entity.Analysis.TotalScoreAnalysis;
import com.xzl.org.learning_situation_analysis.service.AnalysisService;
import com.xzl.org.learning_situation_analysis.service.GroupManageDataService;
import com.xzl.org.learning_situation_analysis.service.PaperService;
import com.xzl.org.learning_situation_analysis.service.QuestionScoreService;
import com.xzl.org.learning_situation_analysis.util.AnalysisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.*;

/**
 * @author 夏智力
 * @FileName analysisController
 * @date 2022/4/15 13:58
 * @Software: IntelliJ IDEA
 */
@Controller
public class analysisController implements AnalysisConstant {

    @Autowired
    private QuestionScoreService questionScoreService;

    @Autowired
    private PaperService paperService;

    @Autowired
    private AnalysisService analysisService;

    @Autowired
    private GroupManageDataService groupManageDataService;


    public void analysis(List<QuestionScore> questionScoreList) {

        Set<Integer> students = new HashSet<>();
        if (!questionScoreList.isEmpty()) {
            for (QuestionScore questionScore : questionScoreList) {
                students.add(questionScore.getStudentId());
                questionScoreService.insertQuestionScore(questionScore);
            }
            int paperId = questionScoreList.get(0).getPaperId();
            Paper paper = paperService.selectById(paperId);
            // 先对个人分析
            for (int studentId : students) {
                //对题型分析
                List<TotalScoreAnalysis> questionTypeScoreAnalyses = analysisService.selectByQuestionType(paperId, studentId);
                for (TotalScoreAnalysis totalScoreAnalysis : questionTypeScoreAnalyses) {
                    float proportion = totalScoreAnalysis.getScore() / totalScoreAnalysis.getTotalScore();
                    GroupManageData data = new GroupManageData();
                    data.setType(AnalysisConstant.TYPE_STUDENT);
                    data.setTypeId(studentId);
                    data.setPaperId(paperId);
                    data.setExamSessionId(paper.getExamId());
                    data.setTargetType(AnalysisConstant.TARGET_TYPE_QUESTION_TYPE);
                    data.setTargetId(totalScoreAnalysis.getTargetId());
                    data.setProportion(proportion);
                    data.setScore(totalScoreAnalysis.getScore());
                    groupManageDataService.insertGroupManageData(data);
                }
                //对知识点分析
                List<TotalScoreAnalysis> knowledgePointsScoreAnalyses = analysisService.selectByKnowledgePoints(paperId, studentId);
                for (TotalScoreAnalysis totalScoreAnalysis : knowledgePointsScoreAnalyses) {
                    float proportion = totalScoreAnalysis.getScore() / totalScoreAnalysis.getTotalScore();
                    GroupManageData data = new GroupManageData();
                    data.setType(AnalysisConstant.TYPE_STUDENT);
                    data.setTypeId(studentId);
                    data.setPaperId(paperId);
                    data.setExamSessionId(paper.getExamId());
                    data.setTargetType(AnalysisConstant.TARGET_TYPE_KNOWLEDGE_POINTS);
                    data.setTargetId(totalScoreAnalysis.getTargetId());
                    data.setProportion(proportion);
                    data.setScore(totalScoreAnalysis.getScore());
                    groupManageDataService.insertGroupManageData(data);
                }
            }

            //试卷总成绩
            List<GroupManageData> clazzData = new ArrayList<>();
            HashMap<Integer, Float> hashMap = questionScoreService.selectPaperTotalScoreByPaperId(paperId);
            for (Map.Entry<Integer, Float> entry : hashMap.entrySet()) {
                GroupManageData data = new GroupManageData();
                data.setType(AnalysisConstant.TYPE_STUDENT);
                data.setTypeId(entry.getKey());
                data.setPaperId(paperId);
                data.setExamSessionId(paper.getExamId());
                data.setTargetType(AnalysisConstant.TARGET_TYPE_PAPER);
                data.setTargetId(paper.getId());
                data.setScore(entry.getValue());
                clazzData.add(data);
            }

            //划分排名
            clazzData.sort((o1, o2) -> (int) (o2.getScore() - o1.getScore()));
            int n = clazzData.size();
            for (int i = 0; i < n; i++) {
                float proportion = (i + 1) / n;
                GroupManageData data = clazzData.get(i);
                data.setProportion(proportion);
                groupManageDataService.insertGroupManageData(data);
            }

            // 对班级分析
            //题目
            List<AggregateAnalysis> aggregateAnalysisList = analysisService.selectAggregateByPaperId(paperId);
            for (AggregateAnalysis aggregateAnalysis : aggregateAnalysisList) {
                GroupManageData data = new GroupManageData();
                data.setType(AnalysisConstant.TYPE_CLAZZ);
                data.setTypeId(aggregateAnalysis.getClazzId());
                data.setPaperId(paperId);
                data.setExamSessionId(paper.getExamId());
                data.setTargetType(AnalysisConstant.TARGET_TYPE_QUESTION);
                data.setTargetId(aggregateAnalysis.getQuestionId());
                data.setProportion(analysisService.selectByQuestionIdAndClazzId(aggregateAnalysis.getQuestionId(), aggregateAnalysis.getClazzId()));
                data.setAvg(aggregateAnalysis.getAvgScore());
                data.setHighestScore(aggregateAnalysis.getMaxScore());
                data.setLowestScore(aggregateAnalysis.getMinScore());
                groupManageDataService.insertGroupManageData(data);
            }

            //知识点
            List<ClazzAnalysis> clazzAnalysisListByKnowledgePoints = analysisService.selectClazzByTargetType(AnalysisConstant.TARGET_TYPE_KNOWLEDGE_POINTS);
            for (ClazzAnalysis clazzAnalysis : clazzAnalysisListByKnowledgePoints) {
                GroupManageData data = new GroupManageData();
                data.setType(AnalysisConstant.TYPE_CLAZZ);
                data.setTypeId(clazzAnalysis.getTypeId());
                data.setPaperId(paperId);
                data.setExamSessionId(paper.getExamId());
                data.setTargetType(AnalysisConstant.TARGET_TYPE_KNOWLEDGE_POINTS);
                data.setTargetId(clazzAnalysis.getTargetId());
                data.setProportion(clazzAnalysis.getProportion());
                data.setAvg(clazzAnalysis.getAvgScore());
                data.setHighestScore(clazzAnalysis.getMaxScore());
                data.setLowestScore(clazzAnalysis.getMinScore());
                groupManageDataService.insertGroupManageData(data);
            }

            //题型
            List<ClazzAnalysis> clazzAnalysisListByQuestionType = analysisService.selectClazzByTargetType(AnalysisConstant.TARGET_TYPE_QUESTION_TYPE);
            for (ClazzAnalysis clazzAnalysis : clazzAnalysisListByQuestionType) {
                GroupManageData data = new GroupManageData();
                data.setType(AnalysisConstant.TYPE_CLAZZ);
                data.setTypeId(clazzAnalysis.getTypeId());
                data.setPaperId(paperId);
                data.setExamSessionId(paper.getExamId());
                data.setTargetType(AnalysisConstant.TARGET_TYPE_QUESTION_TYPE);
                data.setTargetId(clazzAnalysis.getTargetId());
                data.setProportion(clazzAnalysis.getProportion());
                data.setAvg(clazzAnalysis.getAvgScore());
                data.setHighestScore(clazzAnalysis.getMaxScore());
                data.setLowestScore(clazzAnalysis.getMinScore());
                groupManageDataService.insertGroupManageData(data);
            }

            List<ClazzAnalysis> clazzAnalysisListByPaper = analysisService.selectClazzByPaperId(paperId);
            for (ClazzAnalysis clazzAnalysis : clazzAnalysisListByPaper) {
                GroupManageData data = new GroupManageData();
                data.setType(AnalysisConstant.TYPE_CLAZZ);
                data.setTypeId(clazzAnalysis.getTypeId());
                data.setPaperId(paperId);
                data.setExamSessionId(paper.getExamId());
                data.setTargetType(AnalysisConstant.TARGET_TYPE_PAPER);
                data.setTargetId(paperId);
                data.setAvg(clazzAnalysis.getAvgScore());
                data.setHighestScore(clazzAnalysis.getMaxScore());
                data.setLowestScore(clazzAnalysis.getMinScore());
                groupManageDataService.insertGroupManageData(data);
            }


            List<AggregateAnalysis> aggregateAnalysisInGradeList = analysisService.selectAggregateByPaperIdInGrade(paperId);
            for (AggregateAnalysis aggregateAnalysis : aggregateAnalysisInGradeList) {
                GroupManageData data = new GroupManageData();
                data.setType(AnalysisConstant.TYPE_GRADE);
                data.setTypeId(aggregateAnalysis.getClazzId());
                data.setPaperId(paperId);
                data.setExamSessionId(paper.getExamId());
                data.setTargetType(AnalysisConstant.TARGET_TYPE_QUESTION);
                data.setTargetId(aggregateAnalysis.getQuestionId());
                data.setProportion(analysisService.selectByQuestionIdAndGradeIdInGrade(aggregateAnalysis.getQuestionId(), aggregateAnalysis.getClazzId()));
                data.setAvg(aggregateAnalysis.getAvgScore());
                data.setHighestScore(aggregateAnalysis.getMaxScore());
                data.setLowestScore(aggregateAnalysis.getMinScore());
                groupManageDataService.insertGroupManageData(data);
            }

            //知识点
            List<ClazzAnalysis> clazzAnalysisListByKnowledgePointsInGrade = analysisService.selectClazzByTargetTypeInGrade(AnalysisConstant.TARGET_TYPE_KNOWLEDGE_POINTS);
            for (ClazzAnalysis clazzAnalysis : clazzAnalysisListByKnowledgePointsInGrade) {
                GroupManageData data = new GroupManageData();
                data.setType(AnalysisConstant.TYPE_GRADE);
                data.setTypeId(clazzAnalysis.getTypeId());
                data.setPaperId(paperId);
                data.setExamSessionId(paper.getExamId());
                data.setTargetType(AnalysisConstant.TARGET_TYPE_KNOWLEDGE_POINTS);
                data.setTargetId(clazzAnalysis.getTargetId());
                data.setProportion(clazzAnalysis.getProportion());
                data.setAvg(clazzAnalysis.getAvgScore());
                data.setHighestScore(clazzAnalysis.getMaxScore());
                data.setLowestScore(clazzAnalysis.getMinScore());
                groupManageDataService.insertGroupManageData(data);
            }

            //题型
            List<ClazzAnalysis> clazzAnalysisListByQuestionTypeInGrade = analysisService.selectClazzByTargetTypeInGrade(AnalysisConstant.TARGET_TYPE_QUESTION_TYPE);
            for (ClazzAnalysis clazzAnalysis : clazzAnalysisListByQuestionTypeInGrade) {
                GroupManageData data = new GroupManageData();
                data.setType(AnalysisConstant.TYPE_GRADE);
                data.setTypeId(clazzAnalysis.getTypeId());
                data.setPaperId(paperId);
                data.setExamSessionId(paper.getExamId());
                data.setTargetType(AnalysisConstant.TARGET_TYPE_QUESTION_TYPE);
                data.setTargetId(clazzAnalysis.getTargetId());
                data.setProportion(clazzAnalysis.getProportion());
                data.setAvg(clazzAnalysis.getAvgScore());
                data.setHighestScore(clazzAnalysis.getMaxScore());
                data.setLowestScore(clazzAnalysis.getMinScore());
                groupManageDataService.insertGroupManageData(data);
            }

            List<ClazzAnalysis> clazzAnalysisListByPaperInGrade = analysisService.selectClazzByPaperIdInGrade(paperId);
            for (ClazzAnalysis clazzAnalysis : clazzAnalysisListByPaperInGrade) {
                GroupManageData data = new GroupManageData();
                data.setType(AnalysisConstant.TYPE_GRADE);
                data.setTypeId(clazzAnalysis.getTypeId());
                data.setPaperId(paperId);
                data.setExamSessionId(paper.getExamId());
                data.setTargetType(AnalysisConstant.TARGET_TYPE_PAPER);
                data.setTargetId(paperId);
                data.setAvg(clazzAnalysis.getAvgScore());
                data.setHighestScore(clazzAnalysis.getMaxScore());
                data.setLowestScore(clazzAnalysis.getMinScore());
                groupManageDataService.insertGroupManageData(data);
            }

        }


    }
}